"""
Sample answers
--------------

askcli::
	<?xml version="1.0" encoding="UTF-8"?>
	<aptcfg:APTData xmlns:aptcfg="http://www.telecomitalia.it/apt-config_version-1" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><aptcfg:ErrorStatus><aptcfg:ErrorCode>3</aptcfg:ErrorCode>
	</aptcfg:ErrorStatus>
	<aptcfg:CLIList><aptcfg:CLI aptcfg:Status="Registered">0212345678</aptcfg:CLI>
	</aptcfg:CLIList>
	</aptcfg:APTData>
	
setcli with wrong CLI::
	<?xml version="1.0" encoding="UTF-8"?>
	<aptcfg:APTData xmlns:aptcfg="http://www.telecomitalia.it/apt-config_version-1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<aptcfg:ErrorStatus><aptcfg:ErrorCode aptcfg:CLI="0200000000">2</aptcfg:ErrorCode>
	</aptcfg:ErrorStatus>
	</aptcfg:APTData>
	
setcli with registered CLI::
	<?xml version="1.0" encoding="UTF-8"?>
	<aptcfg:APTData xmlns:aptcfg="http://www.telecomitalia.it/apt-config_version-1" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><aptcfg:ErrorStatus>
	<aptcfg:ErrorCode aptcfg:CLI="0212345678">3</aptcfg:ErrorCode>
	</aptcfg:ErrorStatus>
	</aptcfg:APTData>

successful setCLI::
	<?xml version="1.0" encoding="UTF-8"?>
	<aptcfg:APTData xmlns:aptcfg="http://www.telecomitalia.it/apt-config_version-1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><aptcfg:ErrorStatus><aptcfg:ErrorCode aptcfg:CLI="0212345678">0</aptcfg:ErrorCode>
	</aptcfg:ErrorStatus>
	<aptcfg:SIPTerminalParameters><aptcfg:OutboundProxyIP>172.16.1.1</aptcfg:OutboundProxyIP>
	<aptcfg:LineIdentification>0212345678</aptcfg:LineIdentification>
	<aptcfg:SIPkey>deadbeef</aptcfg:SIPkey>
	<aptcfg:SIPDomain></aptcfg:SIPDomain>
	</aptcfg:SIPTerminalParameters>
</aptcfg:APTData>

delallcli::
	<?xml version="1.0" encoding="UTF-8"?>
	<aptcfg:APTData xmlns:aptcfg="http://www.telecomitalia.it/apt-config_version-1" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<aptcfg:ErrorStatus><aptcfg:ErrorCode>0</aptcfg:ErrorCode>
	</aptcfg:ErrorStatus>
	</aptcfg:APTData>

"""

from pivelli.options import o as opts
from pivelli.http import xmlrequest

def delallcli():
	ns, res = xmlrequest('/aptconfig.xml?WINDWEB_URL=%2Fdoc2%2Fdelallcli')
	if res[ns.ErrorStatus][ns.ErrorCode][0] != '0':
		raise Exception, 'Incorrect answer from gateway'

def askcli():
	"""
	Returns a dictionary of boolean values, where the key is the CLI
	and the value is True if the CLI is available for registration,
	False otherwise.
	"""
	ns, res = xmlrequest('/aptconfig.xml?WINDWEB_URL=%2Fdoc2%2Faskcli')
	cli_dict = {}
	for cli in res[ns.CLIList][ns.CLI:]:
		number = str(cli)
		status = cli(ns.Status)
		if status not in ('Registered', 'Available'):
			raise Exception, 'Unknown status "%s" for CLI %s' % (status, number)
		cli_dict[number] = (status == 'Available')
	return cli_dict
	
def setcli(cli_list):
	"""
	Registers the given CLI and returns a quadruple:
	(proxy, username, password, domain) for the VoIP service.
	"""
	# Build parameter list
	setcli_parameters = []
	for i, cli in enumerate(cli_list):
		if i == 0:
			parm_name = 'CLI'
		else:
			parm_name = 'CLI%s' % (i+1)
		setcli_parameters.append(parm_name + '=' + cli)
		
	# Call router
	ns, res = xmlrequest('/aptconfig.xml?WINDWEB_URL=/doc2/setcli&'
		+ '&'.join(setcli_parameters))
	
	# Prepare return dictionary
	cli_data = {}
	
	# Parse error codes
	for status_node in res[ns.ErrorStatus:]:
		code = status_node[ns.ErrorCode]
		cli = code(ns.CLI)
		error = str(code)
		if error == '0':
			status = 'Success'
		elif error == '2':
			status = 'CLI not available to this gateway'
		elif error == '3':
			status = 'CLI already assigned to another device'
		else:
			status = 'Unknown error code' 
		cli_data[cli] = {'error': error, 'status': status}
		
	# Retrieve parameters for successful lines
	for data in res[ns.SIPTerminalParameters:]:
		# Choose sub-dictionary
		cli_dict = cli_data[str(data[ns.LineIdentification])]
		# Fill sub-dictionary
		cli_dict['username'] = str(data[ns.LineIdentification])
		cli_dict['proxy'] = str(data[ns.OutboundProxyIP])
		cli_dict['key'] = str(data[ns.SIPkey])
		cli_dict['domain'] = str(data[ns.SIPDomain])

	return cli_data
